<?php

/*
 * This file is part of Twig.
 *
 * (c) 2009 Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
class Twig_Extension_Sandbox extends Twig_Extension
{
  protected $sandboxedGlobally;
  protected $sandboxed;
  protected $policy;

  public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false)
  {
    $this->policy            = $policy;
    $this->sandboxedGlobally = $sandboxed;
  }

  /**
   * Returns the node transformer instances to add to the existing list.
   *
   * @return array An array of Twig_NodeTransformer instances
   */
  public function getNodeTransformers()
  {
    return array(new Twig_NodeTransformer_Sandbox());
  }

  public function enableSandbox()
  {
    $this->sandboxed = true;
  }

  public function disableSandbox()
  {
    $this->sandboxed = false;
  }

  public function isSandboxed()
  {
    return $this->sandboxedGlobally || $this->sandboxed;
  }

  public function isSandboxedGlobally()
  {
    return $this->sandboxedGlobally;
  }

  public function setSecurityPolicy(Twig_Sandbox_SecurityPolicyInterface $policy)
  {
    $this->policy = $policy;
  }

  public function getSecurityPolicy()
  {
    return $this->policy;
  }

  public function checkSecurity($tags, $filters)
  {
    if ($this->isSandboxed())
    {
      $this->policy->checkSecurity($tags, $filters);
    }
  }

  public function checkMethodAllowed($obj, $method)
  {
    if ($this->isSandboxed())
    {
      $this->policy->checkMethodAllowed($obj, $method);
    }
  }

  public function checkPropertyAllowed($obj, $method)
  {
    if ($this->isSandboxed())
    {
      $this->policy->checkPropertyAllowed($obj, $method);
    }
  }

  /**
   * Returns the name of the extension.
   *
   * @return string The extension name
   */
  public function getName()
  {
    return 'sandbox';
  }
}
